﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SoftwareLogistics.SharepointClient.Models;
using SoftwareLogistics.SharepointClient.Library;
using SoftwareLogistics.SharepointClient.Silverlight.Controls;
using System.Text;

namespace SoftwareLogistics.SharepointClient.Silverlight.Views
{
    public partial class PopupDataForm : ChildWindow
    {
        Guid _dataRowId;
        SPFormData _formData;
        List<SPField> _fields;

        public PopupDataForm()
        {
            InitializeComponent();
        }

        public void Show(SPFormData formData)
        {
            _formData = formData;
            Title = Common.ListMetaData[_formData.ListName].Name;
            _fields = _formData.GetFormFields();
            foreach (var field in _fields)
            {
                var ctrl = new FieldControl();
                ctrl.Field = field;
                FormFields.Children.Add(ctrl);
            }

            Show();
        }

        public void Show(Guid id)
        {
            _dataRowId = id;
            var formData = Common.GetFromNavDictionary<SPFormData>(_dataRowId);
            Show(formData);
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            var errors = _formData.ProcessFields(_fields);
            if (errors.Count() > 0)
            {
                var bldr = new StringBuilder();
                foreach (var error in errors)
                    bldr.AppendLine(error.Message);

                MessageBox.Show(bldr.ToString(), "Please correct the following errors.", MessageBoxButton.OK);

                return;
            }

            var service = new Services.SPListService();
            service.RecordUpdated += (s, args) =>
            {
                Dispatcher.BeginInvoke(() =>
                {                    
                    App.ProgressDialog.Close();
                    DialogResult = true;
                });
            };

            service.FailedUpdatingRecord += (s, args) =>
            {
                Dispatcher.BeginInvoke(() =>
                {
                    App.ProgressDialog.Close();
                    MessageBox.Show("Error updating record, please try again later.");
                });
            };

            if(_formData.RowData != null &&  !string.IsNullOrEmpty(_formData.RowData.ID))
                _fields.Add(new SPField() { Name = "ID", Value = _formData.RowData.ID });

            App.ProgressDialog.Show();
            service.UpdateRecord(_formData.IsEdit, _formData.ListName, _fields);            
        }
    }
}

